home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / softwareproperties / CountryInformation.py < prev    next >
Encoding:
Python Source  |  2009-03-27  |  2.0 KB  |  61 lines

  1. # country.py - provides country based information
  2. #
  3. #  Copyright (c) 2006 FSF Europe
  4. #
  5. #  Author:
  6. #       Sebastian Heinlein <glatzor@ubuntu.com>
  7. #
  8. #  This program is free software; you can redistribute it and/or 
  9. #  modify it under the terms of the GNU General Public License as 
  10. #  published by the Free Software Foundation; either version 2 of the
  11. #  License, or (at your option) any later version.
  12. #
  13. #  This program is distributed in the hope that it will be useful,
  14. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. #  GNU General Public License for more details.
  17. #
  18. #  You should have received a copy of the GNU General Public License
  19. #  along with this program; if not, write to the Free Software
  20. #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. #  USA
  22.  
  23. import os
  24. import gettext
  25. from xml.etree.ElementTree import ElementTree
  26.  
  27. class CountryInformation(object):
  28.   def __init__(self):
  29.     # get a list of country codes and real names
  30.     self.countries = {}
  31.     fname = "/usr/share/xml/iso-codes/iso_3166.xml"
  32.     if os.path.exists(fname):
  33.       et = ElementTree(file=fname)
  34.       it = et.getiterator('iso_3166_entry')
  35.       for elm in it:
  36.         if elm.attrib.has_key("common_name"):
  37.           descr = elm.attrib["common_name"]
  38.         else:
  39.           descr = elm.attrib["name"]
  40.         if elm.attrib.has_key("alpha_2_code"):
  41.           code = elm.attrib["alpha_2_code"]
  42.         else:
  43.           code = elm.attrib["alpha_3_code"]
  44.         self.countries[code] = gettext.dgettext('iso_3166',descr)
  45.     self.country = None
  46.     self.code = None
  47.     locale = os.getenv("LANG", default="en.UK")
  48.     a = locale.find("_")
  49.     z = locale.find(".")
  50.     if z == -1:
  51.         z = len(locale)
  52.     self.code = locale[a+1:z]
  53.     self.country = self.get_country_name(self.code)
  54.  
  55.   def get_country_name(self, code):
  56.     if self.countries.has_key(code):
  57.         name = self.countries[code]
  58.         return name
  59.     else:
  60.         return code
  61.